
Imports System
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for GroupNotifications.
	''' </summary>
	Public Class GroupNotifications
				'
				' Add constructor logic here
				'
		Public Sub New()
		End Sub

		' Send and email to the moderator when a user whats to join a private group
		Public Shared Sub SendMembershipRequestNotification(ByVal gm As GroupMember, ByVal grpID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim toUser As User = Users.GetUserByID(grp.Uid)
			If toUser = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			If Not toUser.EnableGroupEmailNotify Then
				Return
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + toUser.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_NEW_PRIVATE_GROUP_MEMBER_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			' body...
			Dim body As String = Constants.NOTIFY_NEW_PRIVATE_GROUP_MEMBER_BODY.Replace(Constants.TXT_USER_NAME_TAG, gm.User.Name)

			body = body.Replace(Constants.TXT_USER_EMAIL_TAG, gm.User.Email)

			body = body.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			body = body.Replace(Constants.TXT_MEMBERSHIP_REQUEST_LINK_TAG, ComposeMembershipRequestLink(grpID) + "&" + Users.ComposeURLLoginParams(toUser))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to the moderator when a new member joined a public group
		Public Shared Sub SendMembershipNotification(ByVal gm As GroupMember, ByVal grpID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim toUser As User = Users.GetUserByID(grp.Uid)
			If toUser = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			If Not toUser.EnableGroupEmailNotify Then
				Return
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + toUser.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_NEW_PUBLIC_GROUP_MEMBER_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			' body...
			Dim body As String = Constants.NOTIFY_NEW_PUBLIC_GROUP_MEMBER_BODY.Replace(Constants.TXT_USER_NAME_TAG, gm.User.Name)

			body = body.Replace(Constants.TXT_USER_EMAIL_TAG, gm.User.Email)

			body = body.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

				'ComposeGroupLink( grpID ) + "&" + Users.ComposeURLLoginParams( toUser )
			body = body.Replace(Constants.TXT_GROUP_LINK_TAG, ComposeGroupWelcomeLink(grpID) + "&" + Users.ComposeURLLoginParams(toUser))


			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to a user when his membership is accepted
		' by the moderator of the private group he sent membership request previously
		Public Shared Sub SendMembershipAcceptedNotification(ByVal gm As GroupMember, ByVal grpID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			If Not gm.User.EnableGroupEmailNotify Then
				Return
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + gm.User.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_MEMBERSHIP_ACCEPTED_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			' body...
			Dim body As String = Constants.NOTIFY_MEMBERSHIP_ACCEPTED_BODY.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			body = body.Replace(Constants.TXT_SEE_MY_GROUPS_LINK_TAG, ComposeMyGroupsLink() + "?" + Users.ComposeURLLoginParams(gm.User))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(gm.User))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to a user when his membership is canceled by the moderator of the group
		Public Shared Sub SendMembershipModeratorCancelledNotification(ByVal usr As User, ByVal grpID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			If Not usr.EnableGroupEmailNotify Then
				Return
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + usr.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_MEMBERSHIP_CANCELED_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			' body...
			Dim body As String = Constants.NOTIFY_MEMBERSHIP_CANCELED_BODY.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			body = body.Replace(Constants.TXT_SEE_MY_GROUPS_LINK_TAG, ComposeMyGroupsLink() + "?" + Users.ComposeURLLoginParams(usr))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(usr))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to all group members when the group is canceled
		Public Shared Sub SendGroupCanceledMemberNotification(ByVal gm As GroupMember, ByVal grpID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim toUser As User = Users.GetUserByID(gm.UserID)
			If toUser = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + toUser.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_GROUP_CANCELED_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			' body...
			Dim body As String = Constants.NOTIFY_GROUP_CANCELED_BODY.Replace(Constants.TXT_USER_NAME_TAG, gm.User.Name)

			body = body.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			body = body.Replace(Constants.TXT_GROUPS_MAIN_LINK_TAG, ComposeGroupsMainLink() + "?" + Users.ComposeURLLoginParams(gm.User))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to moderator when the group is canceled
		Public Shared Sub SendGroupCanceledModeratorNotification(ByVal gm As User, ByVal grpID As Integer)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim toUser As User = gm
			If toUser = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + toUser.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_GROUP_CANCELED_MODERATOR_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			' body...
			Dim body As String = Constants.NOTIFY_GROUP_CANCELED_MODERATOR_BODY.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			body = body.Replace(Constants.TXT_GROUPS_MAIN_LINK_TAG, ComposeGroupsMainLink() + "?" + Users.ComposeURLLoginParams(toUser))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to moderator when the group is canceled
		Public Shared Sub SendNewGroupCommentModeratorNotification(ByVal gm As User, ByVal grpID As Integer, ByVal fromUser As User)
			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim toUser As User = gm
			If toUser = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + toUser.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_NEW_GROUP_COMMENT_MODERATOR_SUBJECT

			' body...
			Dim body As String = Constants.NOTIFY_NEW_GROUP_COMMENT_MODERATOR_BODY.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)

			body = body.Replace(Constants.TXT_USER_NAME_TAG, fromUser.Name)

			body = body.Replace(Constants.TXT_GROUP_COMMENTS_LINK_TAG, ComposeGroupCommentsLink(grpID) + "&" + Users.ComposeURLLoginParams(toUser))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send email notification to moderator when a new group bulletin is posted
		Public Shared Sub SendNewGroupBulletinModeratorNotification(ByVal gm As User, ByVal grpID As Integer, ByVal fromUser As User)
			Dim toUser As User = gm
			If toUser = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_UNEXPECTED_ERROR)
			End If

			Dim from As String = Constants.MESSAGES_EMAIL
			Dim [to] As String = "<" + toUser.Email + ">"

			' subject...
			Dim subj As String = Constants.NOTIFY_NEW_GROUP_BULLETIN_MODERATOR_SUBJECT.Replace(Constants.TXT_USER_NAME_TAG, fromUser.Name)

			' body...
			Dim body As String = Constants.NOTIFY_NEW_GROUP_BULLETIN_MODERATOR_BODY.Replace(Constants.TXT_USER_NAME_TAG, fromUser.Name)

			body = body.Replace(Constants.TXT_GROUP_BULLETIN_LINK_TAG, ComposeGroupBulletinLink(grpID) + "&" + Users.ComposeURLLoginParams(toUser))

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			' Do send the mail
			Utils.SendMail(from, [to], subj, body)
		End Sub

		' Send an invitation to a friend to join 2bm group
		' NOTE: throws an exception on error (e.g. if the email was wrong and could not be sent)
		' the caller is responsible to intercept it and display a message to the user
		Public Shared Sub SendGroupInvitation(ByVal toUser As User, ByVal customBody As String, ByVal grpInviter As Group)
			Dim from As String = grpInviter.Name + "<" + Constants.INVITATION_EMAIL + ">"
			Dim subj As String = Constants.INVITE_TO_GROUP_MAIL_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grpInviter.Name)
			Dim body As String = ComposeGroupInviteMailBody(customBody, grpInviter, toUser)

			Utils.SendMail(from, toUser.Email, subj, body)
		End Sub

		' Compose invate mail body for group invitations
		Public Shared Function ComposeGroupInviteMailBody(ByVal customBody As String, ByVal grp As Group, ByVal toUser As User) As String
			Dim body As String = Constants.INVITE_TO_GROUP_MAIL_BODY

			' replace the group name and profile link
			body = body.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)
			If toUser <> Nothing Then
				body = body.Replace(Constants.TXT_GRP_PROFILE_LINK_TAG, ComposeGroupProfileLink(grp.ID) + "&" + Users.ComposeURLLoginParams(toUser))
			Else
				body = body.Replace(Constants.TXT_GRP_PROFILE_LINK_TAG, ComposeGroupProfileLink(grp.ID))
			End If

			' insert the custom user text, if there was any
			Dim bodyToSend As String = customBody
			If bodyToSend = Nothing OrElse bodyToSend.Length = 0 Then
				bodyToSend = Constants.INVITE_BODY_NONE
			End If
			body = body.Replace(Constants.TXT_USER_TEXT_TAG, bodyToSend)

			' No SPAM disclaimer
			body = body.Replace(Constants.TXT_DISCLAIMER_TEXT_TAG, Users.ComposeNoSPAMDisclaimer(toUser))

			Return body
		End Function

		Protected Shared Function ComposeMyGroupsLink() As String
			Return Constants.APPLICATION_URL + "/" + Constants.MY_GROUPS
		End Function

		Protected Shared Function ComposeGroupLink(ByVal grpID As Integer) As String
			Return Constants.APPLICATION_URL + "/" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grpID
		End Function

		Protected Shared Function ComposeGroupWelcomeLink(ByVal grpID As Integer) As String
			Return Constants.APPLICATION_URL + "/" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
		End Function

		Protected Shared Function ComposeGroupCommentsLink(ByVal grpID As Integer) As String
			Return Constants.APPLICATION_URL + "/" + Constants.GROUP_COMMENTS_PAGE + "?" + Constants.GROUP_PARAM + "=" + grpID
		End Function

		Protected Shared Function ComposeGroupBulletinLink(ByVal grpID As Integer) As String
			Return Constants.APPLICATION_URL + "/" + Constants.BULLETIN + "?" + Constants.GROUP_PARAM + "=" + grpID
		End Function

		Protected Shared Function ComposeGroupsMainLink() As String
			Return Constants.APPLICATION_URL + "/" + Constants.GROUPS_MAIN
		End Function

		Protected Shared Function ComposeMembershipRequestLink(ByVal grpID As Integer) As String
			Return Constants.APPLICATION_URL + "/" + Constants.VIEW_AWAITING_CONFIRMATION + "?" + Constants.GROUP_PARAM + "=" + grpID
		End Function

		Protected Shared Function ComposeSeeAllFriendsLink() As String
			Return Constants.APPLICATION_URL + "/" + Constants.SEE_ALL_FRIENDS
		End Function

		Protected Shared Function ComposeGroupProfileLink(ByVal grpID As Integer) As String
			Return Constants.APPLICATION_URL + "/" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grpID
		End Function
	End Class
End Namespace






